let mongoose = require('mongoose'),
    resourcesPost = mongoose.model('resources_posts');

module.exports = (req, res) => {
    let params = req.query,
        condition = {};

    // 查询条件组合
    params.pageNo = parseInt(params.pageNo) || 1;
    params.pageSize = parseInt(params.pageSize) || 20;

    if(params.key){
        condition.title = new RegExp(params.key);
    }
    if(params.userName){
        condition.userName = new RegExp(params.userName);
    }
    if(params.recommend){
        condition.recommend = true;
    }
    if(params.resourcesType){
        condition.resourcesType = new RegExp(params.resourcesType);
    }
    switch(parseInt(params.status)){
        case 0: break;
        case 1: case 2: case 3: case 4:
            condition.status = parseInt(params.status);
            break;
        default:
            condition.status = {
                "$gte": 1,
                "$lte": 3
            }
            break;
    }
    if(params.releaseData){
        condition.createTime = {
            "$gte": new Date(params.releaseData[0]),
            "$lte": new Date(params.releaseData[1])
        }
    }

    // 查询并返回
    resourcesPost.find(condition)
    .skip((params.pageNo - 1) * params.pageSize || 0)
    .limit(params.pageSize)
    .sort({'createTime': -1})
    .exec((err, data) => {
        if(err){
            res.json(resFail(err))
        }else{
            let page = {
                    pageNo: params.pageNo,
                    pageSize: params.pageSize,
                    total: data.length
                }
            res.json({
                result: 1,
                data: {
                    list: data,
                    page: page
                }
            })
        }
    })
}